/**
 * Created by yangzs on 2014-11-28.
 */
var ng = require('nodegrass');
var mysql = require('mysql');
var EventProxy = require('eventproxy');
var domain=require('domain');
var re5s;
var btre;
var pool = mysql.createPool({
    host: 'localhost',
    user: 'root',
    password: '159456',
    database: 'longzs',
    connectionLimit: 30
});
var fs = require('fs');
async = require("async");
var res;
var cookie;
var host;
var didata;
var endstr;
//http://www.xooo8.com/AV/yazhouxingai/index1.html
var exec = require('child_process').exec;
var header = {'Cookie': cookie};
var s = '<!DOCTYPE html PUBLIC " -//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><meta http-equiv="X-UA-Compatible" content="IE=7" /><link href="http://collect2.longsunhd.com/pbl.css" rel="stylesheet" type="text/css" media="all" /><script src="http://liuxiaofan.com/demo/js/jquery-1.6.4.min.js" type="text/javascript"></script></head><body><div class="lxf-box"><ul>';
var bianma = "utf-8";
var listhreftre;
var firstlisturl;
var thenrre;
var callback;
var host;
var s2;
var pagenn;
var pagesize;


function getbasehost(url) {
    var re = new RegExp("http\\:\\/\\/(?:[^<>\\.]+?\\.)?([^<>\\/]+?(?!www)(\\.(com|gov|edu|org|mobi|la)(?=\\/|$|\\.cn)|\\.cn(?=\\/|$|\\:)|\\.net|\\.cc|\\.tv|\\.hk|\\.tw)+)", "i");
    nn = re.exec(url);
    basehost = nn[1];
    var re2 = new RegExp("^(?:[^<>]*?\\.)*?([a-z0-9_]+?\\.(com|gov|edu|org|cn|mobi|la))");
    basehost = basehost.replace(re2, RegExp.$1);
    return basehost;
}

function quanurl(suburl){
    suburl=(suburl.substr(0,1)!="."&&suburl.substr(0,1)!="/"&&suburl.substr(0,7)!="http://")?"./"+suburl:suburl;
    hre = new RegExp("^http\\:\\/\\/", "i");
    url = (hre.test(suburl)) ? suburl : "http://" + host + suburl;
    return url;
}

function retore(re) {
    //tre=re.replace(/\\/g,"\\\\");
    tre = re.replace(/\.\*/g, '[\\s\\S]*');
    tre = tre.replace(/\.\+/g, '[\\s\\S]+');
    tre = tre.replace(/\?\?/g, '?');
    tre = tre.replace(/\+/g, '+?');
    tre = tre.replace(/\*/g, '*?');
    tre = tre.replace(/\)\?/g, ')');
    tre = tre.replace(/\?\?/g, '');
    //tre=tre.replace(/\\\\"/g,'\\\\\\"');
    return tre;
}


function getlongzsre(url, type,callback){
    var fieldname = "";
    switch (type) {
        case 0:
            fieldname = "titlere";
            break;
        case 1:
            fieldname = "re";
            break;
        case 2:
            fieldname = "hrefbtre";
            break;
    }
    ng.get(url, function (data, status, headers) {
    pool.getConnection(function (err, connection) {
        basehost = getbasehost(url, data, fieldname);
        connection.query("SELECT " + fieldname + " as re FROM longzs_re where host='" + basehost + "'", function (err, result) {
            connection.release();
            if (err) {
                throw err;
            }
            if (result.length < 1) {
                return;
            }
            result[0].re = result[0].re.replace(/^\s+|\s+$/, "");
            linere = new RegExp("^[^\\r\\n]+$", "igm");
            //line = linere.exec(result[0].re);
            //line = linere.exec(result[0].re);
            while ((line = linere.exec(result[0].re)) != null) {
                re5ss = new RegExp(retore(line[0]), "ig");
                if ((aurl=re5ss.exec(data))!=null) {
                    pagesi=data.match(re5ss).length;
                    callback(err,retore(line[0]),aurl[1],pagesi);
                    break;
                }
            }
        });
    });
    }, header, 'utf-8').on('error', function (e) {
        console.log("Got error: " + e.message);
    });
};


function profile(s_url, callback) {
    var hostre = new RegExp("http\\:\\/\\/([^\\/]+)\\/");
    hostnn = s_url.match(hostre);
    host = hostnn[1];
    getPublic(s_url, function () {
        var a = 1;
        s2=s;
        //当为isend赋值了多少次之后。
        proxy.after("isend", (pagesize * (Math.floor(pagenn[2]) - Math.floor(pagenn[1])+1)-5), function (isend) {
            //console.log(isend[(isend.length-1)]);
            callback(isend[(isend.length - 1)])
        });
        if (pagenn != null) {
            var burl = s_url.replace(pagenn[0], pagenn[1]);
            for (var i = pagenn[1]; i <= pagenn[2]; i++) {
                var aurl = s_url.replace(pagenn[0], i);
                console.log(aurl);
                ng.get(aurl, function (data, status, headers) {
                    zqzw(listhreftre, data);
                }, header, 'utf-8').on('error', function (e) {
                    console.log("Got error: " + e.message);
                });
            }
        }
    });
}

//抓取正文
function zqzw(btre, data) {
    if (!btre) return;
    listre = new RegExp(btre, "gi");
    n = 0
    while ((urls = listre.exec(data)) != null) {
        urls[1]=quanurl(urls[1])
        //collecturl = "http://collect2.longsunhd.com/collect/index.php?gather_type=2&s_url=" + encodeURIComponent(urls[1]);
        //console.log(urls[1]);

        n++;
        ng.get(urls[1], function (data2, status, headers) {

            result = JSON.parse(data2);
            var re = /(http\:\/\/[^<>\s]+)/gi
            while (res = re.exec(result.message)) {
                s2 += "<li><img src=\"" + res[1] + "\"></li>\r\n";
            }
            proxy.emit("isend", s2);
        }, header, 'utf-8').on('error', function (e) {
            proxy.emit("isend", s2);
            console.log("error:" + e.message);
        });
    }
}



//找到适应文章的正文匹配正则
function getPublic(s_url,callback) {
    //找出一条要抓取的列表页网页
    var reg = new RegExp("\\[([0-9]+)\\-([0-9]+)\\]");
    if (reg.test(s_url)) {
        pagenn = reg.exec(s_url);
        firstlisturl = s_url.replace(pagenn[0], pagenn[1]);
    } else {
        pagenn=[1-1,1,1];
        firstlisturl = s_url;
    }
    //根据列表页
    async.waterfall([
        function (callback){
            getlongzsre(firstlisturl,2,callback);
        },
        function (listurlre,nrurl,pagesi,callback){
            listhreftre=listurlre;
            pagesize=pagesi
            getlongzsre(quanurl(nrurl), 1,callback);
        }
    ], function (err, nrre) {
        thenrre=nrre
        console.log("getPublic end!");
        callback();
    });
}

var proxy = new EventProxy();
collect = function (req, res) {
    var reqd = domain.create();
    reqd.add(req);
    reqd.add(res);
    reqd.on('error', function(er) {
        console.error('Error', er, req.url);
        try {
            res.writeHead(500);
            res.end('Error occurred, sorry.');
        } catch (er) {
            console.error('Error sending 500', er, req.url);
        }
    });
  profile(req.query.s_url,function(data){
        res.send(data);
  });
}
/*
//
var express=require('express');
var app=express();
app.get('/collect/*', collect);
app.listen(8080);
console.log("listen to 8080");
*/
//http://collect2.longsunhd.com:8080/collect/?s_url=http://www.339zz.com/htm/piclist9/[1-2].htm
//http://collect2.longsunhd.com:8080/collect/?s_url=http://sisise.net/artyz/index[1-5].html
//http://collect2.longsunhd.com:8080/collect/?s_url=http://news.gxnews.com.cn/staticpages/20150410/newgx552700a4-12562577.shtml
profile("http://sisise.net/artyz/index[1-1].html",function(data){
    console.log(data);
})



